package com.enzoism.java;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.jms.Destination;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: enzoism
 * Date: 2019/6/26- 0:52
 */
@Controller
public class ActiveMQController {
    @Autowired
    private AlarmProducer alarmProducer;

    @RequestMapping(value="/hello",method= RequestMethod.GET)
    public String testHello(HttpServletRequest request) {
        request.setAttribute("name", "enzoism");
        return "hello";
    }

    @RequestMapping(value="/simpleAlarm",method= RequestMethod.GET)
    public String simpleAlarm(HttpServletRequest request) {
        // 模拟设备报警数据
        String deviceName = "enzo";
        List<String> alarmStrList = initWithData(deviceName);
        System.out.println("设备"+deviceName+"出围栏报警");

        // 报警信息写入数据库
        System.out.println("报警数据写入数据库。。。");

        // 写入消息队列
        Destination destination = new ActiveMQQueue(Constants.RECEIVE_SIMPLE_QUEUE);
        for (String alarmStr : alarmStrList) {
            alarmProducer.sendMessage(destination, alarmStr);
        }
        request.setAttribute("name", "单向通知队列");
        return "hello";
    }

    @RequestMapping(value="/doubleAlarm",method= RequestMethod.GET)
    public String doubleAlarm(HttpServletRequest request) {
        // 模拟设备报警数据
        String deviceName = "enzo";
        List<String> alarmStrList = initWithData(deviceName);
        System.out.println("设备"+deviceName+"出围栏报警");

        // 报警信息写入数据库
        System.out.println("报警数据写入数据库。。。");

        // 写入消息队列
        Destination destination = new ActiveMQQueue(Constants.RECEIVE_DOUBLE_QUEUE);
        for (String alarmStr : alarmStrList) {
            alarmProducer.sendMessage(destination, alarmStr);
        }
        request.setAttribute("name", "双向通知队列");
        return "hello";
    }

    // 模拟初始化数据
    private List<String> initWithData(String devicename){
        List<String> alarmStrList = new ArrayList<>();
        alarmStrList.add(devicename+"out fence01");
        alarmStrList.add(devicename+"out fence02");
        alarmStrList.add(devicename+"in fence01");
        alarmStrList.add(devicename+"in fence02");
        return  alarmStrList;
    }

}

